/*
 * common.h
 *
 *  Created on: 2009-5-13
 *      Author: hwg
 */

#ifndef COMMON_H_
#define COMMON_H_

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#define _LINE_ 2
#define CHARBUFFERSIZE 1000
#define	INTBUFFERSIZE 1000
#define STATEBUFFSIZE 1000
#define EXPBUFFSIZE 20
#define error -1
#define ok 1
#define can 2
#define ad 3
#define orr  4
#define nu  5
#define one	6
#define over 20
#define ncomp 21
#define complete 22
typedef int Status;

//堆栈长度
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10

typedef struct {
	char name[10];
	int age;
	char sex[4];
}people;

typedef struct{
	int top;
	int base;
	char stack[CHARBUFFERSIZE];
}cstack;

typedef struct{
	int top;
	int stack[INTBUFFERSIZE];
}instack;

typedef struct{
	cstack keys;
	cstack words;
	cstack serror;
	instack state;
	instack nextkeynode;
	instack wordscallback;
	instack mode;
	int su;
}lex;
//key链表
typedef struct keynode{
	char words[CHARBUFFERSIZE];
	char exp[EXPBUFFSIZE];
	struct keynode * next;
}keynode;
//words链表
typedef struct wordnode{
	char words[CHARBUFFERSIZE];
	int order;
	char exp[EXPBUFFSIZE];
	char value[10];
	struct wordnode * next;
}wordnode;
//缓冲
typedef struct{
	char *buff;
	int current;
	int count;
	int size;
}buff;
//串
/*
typedef struct{
	char *ch;
	int length;
}HString;*/
//********************************
//********************************
//******************************** data struct
typedef struct{
	char identyfy;
	char action;
	int number;
	int go;
}elmAction;

//********************************

//********************************Production
typedef struct productnode{
	int index;
	char h;
	char express[EXPBUFFSIZE];
	char middlecode[EXPBUFFSIZE];
	int dot;
}productnode;
//********************************

//********************************linklist
typedef union{
	elmAction uniaction;
	productnode	uniproduction;
	wordnode	uniwordnode;
	keynode		unikeynode;
} ElemType;

typedef struct tagNode{
	ElemType data;
	struct tagNode *next;
}LNode, *LinkList;
Status InitList_L(LinkList *L);
Status ListInsert_L(LinkList L, int i, ElemType e);
Status ListInsertbutton_Sq(LinkList L, ElemType e);
Status initlist_keylist(LinkList *L , char *string);
int initlist_wordlist(LinkList *L , char *string);
void printf_keylist(LinkList L);
void printf_wordlist(LinkList L);
Status	Listgetnode(LinkList L, int i, ElemType *e);
Status	Listsetnode(LinkList L, int i, ElemType *e);
Status	searchAction_Sq(LinkList L, char iden ,ElemType *e);
Status	getWord_Sq(LinkList L, int i, ElemType *e);
int initlist_gramexplist(LinkList *L , char *string);
void printf_gramexplist(LinkList L);
//********************************
//********************************SStack
typedef union elemtype_c{
	char data[10];
}elemtype_c;
typedef struct{
	elemtype_c *base;
	elemtype_c *top;
	int stack_size;
}sstack;
Status InitStack_C(sstack *S);
Status Push_C(sstack *S, elemtype_c e);
Status Pop_C(sstack *S, elemtype_c *e);
Status GetTop_C(sstack *S, elemtype_c *e);
Status StackEmpty_C(sstack *S);
//********************************


//********************************ActionLine
typedef struct{
	int statenum;
	LinkList table;
	LinkList explist;
}actionLine;
int InitAction(actionLine * actionc, int statenum);
int getAction(actionLine * actionc, char ident ,elmAction *e);
int addAction(actionLine * actionc, elmAction e);
Status	searchAction_stl(actionLine L, char iden ,ElemType *f);
void printf_Action(LinkList L);

//********************************



//********************************state
typedef union{
	actionLine unistate;
} Elem;

typedef struct statenode{
	Elem data;
	struct statenode *next;
}statenode, *statelink;

Status initlist_state(statelink *L , char *string);
Status isexsits_st(statelink L , int num ,statenode *snode);
Status Insert_St(statelink L, statenode e);
Status InitList_st(statelink *L);
Status getAction_st(statelink L , int statenum , char ident , elmAction *elAction);
void printf_st(statelink L);
//********************************
//********************************Grammar table
typedef struct grammer{
	char S;
	statelink 	action;
	statelink	gotos;
	LinkList	production;
	LinkList	sentence;
	instack		state;
	cstack		symbol;

} grammer;
int agreement(grammer *g ,elmAction cuAction ,int *senorder);
void initles_grammer(grammer *g , char *stateini ,char * actionini, char *producini , LinkList sentenceini);
int grammer_make(grammer *g);
//********************************
//buff
char buffsource[1891];
char buffconfig[1891];
char bufflite[1891];
char buffgotos[1891];
char buffgramm[1891];

/*文件部分 start*/
void my_err(const char * err_string, int line);
int my_read(int fd, char *read_buff);
int openfile(char * dir);
int writetofile(int fd, char *buff);
int createfile(char *buff);
/*文件部分 end*/

/*堆栈部分 start*/
int inpush(instack *s , int n);
int inpop(instack *s);
int ingettop(instack *s);
void ininitian(instack * s);

int cpush(cstack *s , char n);
void cinitian(cstack *s);
char cpop(cstack *s);
char cgettop(cstack *s);
int cinitia(cstack *s ,char *buff);
/*堆栈部分 end*/
/*lex 部分 start*/
char getotheropration(cstack *s, int *next);
int lex_make(lex *session);
void initles(lex *session, char * keys, char* words);
int isend(lex *session);
void match(LinkList word , LinkList key);
/*lex 部分 end*/
/*buff*/
int initbuff( buff *bu, char *header);
int getkeynode(buff *bu , keynode *node);
int getwordsnode(buff *bu , wordnode *node);
int getactionnode(buff *bu ,int *ident, elmAction *node);
int getgramexp(buff *bu , productnode *node);
int isavliablebuff(buff *bu , int i);
void getothercodeindex(buff *bu , int *i);
#endif /* COMMON_H_ */
